<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @package phpbb3 :: Game Console Mod
* @version $Id: gc_functions.php 1.0.2 12-10-2009 Blackpawn $
* @copyright (c) 2004-2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

if (!defined('GC_MOD'))
{
	exit;
}

function gc_check_game_name($name)
{
	global $db;

	$name = utf8_clean_string($name);
	
	$sql = "SELECT game_name
	FROM " . GC_GAMES_TABLE . "
	WHERE LOWER(game_name) = '$name'";

	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	if($row)
	{
		return TRUE;
	}
	
	return False;	
	
}

function gc_check_game_path($path)
{
	global $db;
	
	$path = utf8_clean_string($path);
	
	$sql = "SELECT game_path
	FROM " . GC_GAMES_TABLE . "
	WHERE LOWER(game_path) = '$path'";

	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	if($row)
	{
		return TRUE;
	}
	
	return False;	
	
}

function gc_fetch_users_name($user_id)
{
	global $db;

	if($user_id)
	{	
		$sql = "SELECT *
		FROM " . USERS_TABLE . "
		WHERE user_id = '$user_id'";
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		return $row['username'];
	}
}

function gc_fetch_gid($game_path)
{
	global $db, $phpbb_root_path, $phpEx;

	if(!$game_path)
	{
		redirect(append_sid("{$phpbb_root_path}games/index.$phpEx"));
	}
		
	$sql = "SELECT *
	FROM " . GC_GAMES_TABLE . "
	WHERE game_path = '$game_path'";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);

	return $row['id'];
}

function gc_fetch_game_data($gameid)
{
	global $db;
	$sql = "SELECT *
	FROM " . GC_GAMES_TABLE . "
	WHERE id = '$gameid'";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);

	$data = array(
	'id' 			=> $row['id'],
	'name' 			=> $row['game_name'],
	'descript' 		=> $row['game_descript'],
	'ver_major' 	=> $row['game_ver_major'],
	'ver_minor' 	=> $row['game_ver_minor'],
	'ver_build' 	=> $row['game_ver_build'],
	'round' 		=> $row['game_round'],
	'round_length' 	=> $row['game_round_length'],
	'ticks_left' 	=> $row['game_ticks_left'],
	'time_mgt' 		=> $row['game_time_mgt'], 
	'members' 		=> $row['game_members'],
	'member_slots' 	=> $row['game_member_slots'],
	'path' 			=> $row['game_path'],
	'bulletin' 		=> $row['game_bulletin'],
	'bulletin_show' => $row['game_bulletin_show'],
	'online' 		=> $row['game_online'],
	'open' 			=> $row['game_open'],
	'author' 		=> $row['game_author'],
	'email' 		=> $row['game_email'],
	'website' 		=> $row['game_website'],
	'credits' 		=> $row['game_credits'],
	'history' 		=> $row['game_history'],
	'license'		=> $row['game_license'],
	'copyrights'	=> $row['game_copyrights'],
	'tables'		=> $row['game_tables'],
	'gui'			=> $row['game_unique_id'],
	);  		

	$db->sql_freeresult($result);
	
	return $data;
}

function gc_check_game_unique_id($game_unique_id)
{
	
	global $db;
	$sql = "SELECT game_unique_id
	FROM " . GC_GAMES_TABLE . "
	WHERE game_unique_id = '$game_unique_id'";

	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	if($row)
	{
		return TRUE;
	}
	
	return False;	
}

function gc_remove_game($gameid)
{
	global $db,$user;
	
	if(isset($gameid))
	{
		// Remove the selected games tables					
		$game = gc_fetch_game_data($gameid);
		
		// Sort out the tables
		$tables = explode('|', $game['tables']);
		
		//count the table to be removed
		$table_count = count($tables);
		
		// Remove each table
		for($i=0; $i < $table_count; ++$i)
		{
			$sql_drop = "DROP TABLE IF EXISTS `$tables[$i]`;";
			$db->sql_query($sql_drop);
			$sql_drop = NULL;							
		}
			
		// Remove the main game listing table
		$db->sql_query('DELETE FROM ' . GC_GAMES_TABLE . " WHERE id = $gameid");
	
		// Remove the data from the accounts table
		$db->sql_query('DELETE FROM ' . GC_ACCOUNTS_TABLE . " WHERE game_id = $gameid");
	}
}

function gc_fetch_game_option_list()
{
	global $db,$user;
	$count = NULL;
	$gc_game_option_list = NULL;
	
	$sql = 'SELECT id, game_name
	FROM ' . GC_GAMES_TABLE . '
	ORDER BY id ASC';
	$result = $db->sql_query($sql);

	while ($row = $db->sql_fetchrow($result))
	{
		$gc_game_option_list .= '<option value="'.$row['id'].'">'.$row['game_name'].'</option>';
		++$count;
	}
	
	if($count < 1){
		$gc_game_option_list = '<option>'.$user->lang['NO_GAMES_INSTALLED'].'</option>';
	}
	
	$db->sql_freeresult($result);

	return $gc_game_option_list;
}

function gc_check_account_status($user_id,$game_id)
{
	global $db;
	$account_status = 0;
	
	$sql = 'SELECT *
	FROM ' . GC_ACCOUNTS_TABLE . '
	WHERE user_id = '.$user_id.' 
	AND game_id = '.$game_id;
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);

	if($row)
	{
		$account_status = 1;
	}
	
	return $account_status;
}

function gc_fetch_game_count()
{
	global $db;

	$sql = 'SELECT count(id) as total_games
	FROM ' . GC_GAMES_TABLE . '
	ORDER BY id ASC';
	$result = $db->sql_query($sql);
	$total_games = (int) $db->sql_fetchfield('total_games');
	$db->sql_freeresult($result);

	return $total_games;

}
	
function gc_fetch_games()
{
	global $db, $config;

	$sql = 'SELECT * 
	FROM ' . GC_GAMES_TABLE . '
	ORDER BY id ASC';
	$result = $db->sql_query($sql);
	
	while ($row = $db->sql_fetchrow($result))
	{
		$gid = $row['id'];
		$gc_game_list [$gid] = array(
		'id' 			=> 	$row['id'],
		'name' 			=> 	$row['game_name'],
		'descript' 		=> 	$row['game_descript'],
		'ver_major' 	=> 	$row['game_ver_major'],
		'ver_minor' 	=> 	$row['game_ver_minor'],
		'ver_build' 	=> 	$row['game_ver_build'],
		'round' 		=> 	$row['game_round'],
		'round_length' 	=> 	$row['game_round_length'],
		'ticks_left' 	=> 	$row['game_ticks_left'],
		'time_mgt' 		=> 	$row['game_time_mgt'], 
		'members' 		=> 	$row['game_members'],
		'member_slots' 	=> 	$row['game_member_slots'],
		'path' 			=> 	$row['game_path'],
		'bulletin' 		=> 	$row['game_bulletin'],
		'bulletin_show' => 	$row['game_bulletin_show'],
		'online' 		=> 	$row['game_online'],
		'open' 			=> 	$row['game_open'],
		'author' 		=> 	$row['game_author'],
		'email' 		=> 	$row['game_email'],
		'website' 		=> 	$row['game_website'],
		'credits' 		=> 	$row['game_credits'],
		'history' 		=> 	$row['game_history'],
		'license' 		=> 	$row['game_license'],
		'copyrights' 	=> 	$row['game_copyrights'],

		);  		
	}	
	$db->sql_freeresult($result);

	return $gc_game_list;
}

function load_custom_css()
{
	global $db, $user, $game_config;

	// Text color
	$gc_text_color = $game_config['text_color'];
	
	// Link Color
	$gc_link_text_color = $game_config['link_text_color'];
	
	// Visited Link Color
	$gc_visited_text_color = $game_config['visited_text_color'];
	
	// Hover Link Color
	$gc_hover_text_color = $game_config['hover_text_color'];
	
	// Active Link Color
	$gc_active_text_color = $game_config['active_text_color'];
	
	// Header Bar Text Color
	$gc_headerbar_text_color = $game_config['headerbar_text_color'];

	// Header Bar Background Color
	$gc_headerbar_bg_color = $game_config['headerbar_bg_color'];
	
	// Announcement Bar Text Color
	$gc_announcementbar_text_color = $game_config['announcementbar_text_color'];

	// Announcement Bar Background Color
	$gc_announcementbar_bg_color = $game_config['announcementbar_bg_color'];
	
	$gc_row_1 = $game_config['gc_row_1'];
	$gc_row_2 = $game_config['gc_row_2'];
	$gc_row_3 = $game_config['gc_row_3'];

	// Table Border Color
	$gc_table_border_color = $game_config['gc_table_border_color'];
	
	// Panel Background Color
	$gc_panel_bgcolor = $game_config['gc_panel_bgcolor'];

	$custom_css = '
	<style type="text/css"> 
	body{
		color: #'.$gc_text_color.';
	}
	
	a:link { color: #'.$gc_link_text_color.'; text-decoration: none;}
	a:visited { color: #'.$gc_visited_text_color.'; text-decoration: none; }
	a:hover	{ color: #'.$gc_hover_text_color.'; text-decoration: underline; }
	a:active { color: #'.$gc_active_text_color.'; text-decoration: none; }
		
	.gc_headerbar 
	{
		background: #'.$gc_headerbar_bg_color.' none repeat-x 0 0;
		color: #'.$gc_headerbar_text_color.';
		margin-bottom: 4px;
		padding: 0 5px;
	}
	
	.gc_announcements
	{
		background: #'.$gc_announcementbar_bg_color.' none repeat-x 0 0;
		color: #'.$gc_announcementbar_text_color.';
		margin-bottom: 4px;
		padding: 0 5px;
	}
		
	.gc_row_1
	{
		margin-bottom: 5px;
		padding: 5px;
		background-color: #'.$gc_row_1.';
		color: #'.$gc_text_color.';
	}
		
	.gc_row_2
	{
		margin-bottom: 5px;
		padding: 5px;
		background-color: #'.$gc_row_2.'; 
		color: #'.$gc_text_color.';
	}
		
	.gc_row_3
	{
		margin-bottom: 5px;
		padding: 5px;
		background-color: #'.$gc_row_3.'; 		
		color: #'.$gc_text_color.';
	}
	
	.gc_table_border_color
	{
		border: solid 2px  #'.$gc_table_border_color.';
	}
		
	.gc_panel
	{
		margin: 2px; 
		background-color: #'.$gc_panel_bgcolor.'; 	
		color: #'.$gc_text_color.';
	}

	.gc_copyrights
	{
		color: #'.$gc_headerbar_text_color.';
	}
		
	a.gc_copyrights
	{
		color: #'.$gc_headerbar_text_color.';
	}
	</style>



	';

	return $custom_css;
}

function error_msgs($error_msgs, $error_header = Null)
{
	global $db, $user;

	if(isset($error_msgs))
	{
		$error_msg = '
		<style type="text/css"> 
		<!--
		#container 
		{
			padding: 5px 5px 5px 5px; 
			min-width: 300px; 
		}

		.gc_epanel 
		{
			margin: 2px; 
			background-color: #ffffff; 
			border: solid 1px  #DE0606; 
		}

		#container h1 
		{
			padding: 5px 5px 5px 5px; 
			margin: 2px; 
			font: bold 1.2em "Lucida Grande", Arial, Helvetica, sans-serif; 
			text-decoration: none;
			color: #FFFFFF; 
			background-color: #DE0606; 
		}

		#container div 
		{
			margin-top: 5px;
			margin-bottom: 5px;
			margin-left: 5px;
			margin-right: 5px;
			font: bold 1.2em "Lucida Grande", Arial, Helvetica, sans-serif;
			text-decoration: none;
			text-align: left;
			color: #333333;
			background-color: #ECF1F3; 
		}
		-->
		</style>';

		if(!$error_header){
			$error_header = $user->lang['ERROR_MSG_HEADER'].'<br>';
		}else{
			$error_header = $error_header.'<br>';
		}
		
		$error_msg .= '<div id="container">';
		$error_msg .= '<div class="gc_epanel">';
		$error_msg .= '<h1>'.$error_header.$error_msgs.'</h1>';
		$error_msg .= '</div>';
		$error_msg .= '</div>';
		$error_msg .= '<br>';
		$error_msg .= '<br>';

		return $error_msg;
	}
}

function set_game_config($config_name, $config_value)
{
	define('GC_MOD', true);
	global $db, $cache, $game_config;

	$sql = 'UPDATE ' . GC_CONFIG_TABLE . "
		SET config_value = '" . $db->sql_escape($config_value) . "'
		WHERE config_name = '" . $db->sql_escape($config_name) . "'";
	$db->sql_query($sql);

	if (!$db->sql_affectedrows() && !isset($game_config[$config_name]))
	{
		$sql = 'INSERT INTO ' . GC_CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
			'config_name'	=> $config_name,
			'config_value'	=> $config_value));
		$db->sql_query($sql);
	}

	$config[$config_name] = $config_value;
}

?>